/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.dao;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;

import com.tech15.core.model.Attach;
import com.tech15.core.vo.query.AttachQuery;

@Repository
public class AttachDao extends BaseHibernateDao<Attach,java.lang.String>{

	public Class getEntityClass() {
		return Attach.class;
	}
	
	public Page findPage(AttachQuery query) {
		String sql = "select t from Attach t where 1=1 "
			  	+ "/~ and t.name = {name} ~/"
			  	+ "/~ and t.path = {path} ~/"
			  	+ "/~ and t.attachSize = {attachSize} ~/"
			  	+ "/~ and t.contType = {contType} ~/"
			  	+ "/~ and t.postfix = {postfix} ~/"
			  	+ "/~ and t.userid = {userid} ~/"
				+ "/~ and t.upDate >= {upDateBegin} ~/"
				+ "/~ and t.upDate <= {upDateEnd} ~/"
			  	+ "/~ and t.namespace = {namespace} ~/"
			  	+ "/~ and t.tableid = '{tableid}' ~/"
				+ "/~ order by [sortColumns] ~/";

		return pageQuery(sql,query);
	}
	
	public List<Attach> findByTableNameAndTableId(String tablename, String tableid) {
		String hql = "select t from Attach t where tablename='"+tablename+"' and tableid='"+tableid+"'";
		return getHibernateTemplate().find(hql);
	}

	public List<Attach> findByTableNameAndId(String tablename, String id) {
		/*String hql = "from Attach t where t.tablename=? and t.tableid=?";
		return getHibernateTemplate().find(hql,new Object[]{tablename,id});*/
		String sql = "select t.*,t.attach_size as attachSize,t.up_time as upTime,t1.realname as username from attach t left join sys_user t1 on t.userid=t1.userid where t.tablename='"+tablename+"' and t.tableid='"+id+"' order by t.up_time asc";
		Map map = new HashMap();
		map.put("attachid","String");
		map.put("name","String");
		map.put("attachSize", "Long");
		map.put("userid", "Long");
		map.put("username", "String");
		map.put("upTime","Timestamp");
		map.put("path","String");
		map.put("tablename", "String");
		map.put("tableid","String");
		return findSqlAll(sql, map, Attach.class);
	}
	

}
